// BrowserDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

#include "OwnTree.h"
#include "FileInfo.h"
#include "ShowDlg.h"
#include "NewFolderDlg.h"
#include "RenameDlg.h"
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

#define WM_UPLOAD_START			(WM_USER+11)
#define WM_UPLOAD_FINISH		(WM_USER+12)
#define WM_DOWNLOAD_START		(WM_USER+13)
#define WM_DOWNLOAD_FINISH		(WM_USER+14)

typedef list<FileStruct> LIST_FS;

extern HWND g_hWndParent;

struct UploadFileInfo
{
	char* lpFileName;
	char* lpCurDirID;
	HWND  hWnd;
	BOOL  bCover;
	char* lpDeleteFileID;
};

struct DownloadFileInfo
{
	char* lpFileID;
	char* lpFileDir;
	HWND  hWnd;
};

struct DirInfo
{
	string sDirName;
	string sDirID;
};

class CBrowserDlg :	public CDialogImpl<CBrowserDlg>, 
					public CUpdateUI<CBrowserDlg>,
					public CMessageFilter, 
					public CIdleHandler
{
public:
	enum { IDD = IDD_BROWSER };

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		return CWindow::IsDialogMessage(pMsg);
	}

	BOOL OnIdle()
	{
		UIUpdateChildWindows();
		return FALSE;
	}

	BEGIN_UPDATE_UI_MAP(CBrowserDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CBrowserDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		MESSAGE_HANDLER(WM_CLOSE, OnClose)
		MESSAGE_HANDLER(WM_UPLOAD_START, OnUploadStart)
		MESSAGE_HANDLER(WM_UPLOAD_FINISH, OnUploadFinish)
		MESSAGE_HANDLER(WM_DOWNLOAD_START, OnDownloadStart)
		MESSAGE_HANDLER(WM_DOWNLOAD_FINISH, OnDownloadFinish)	
		COMMAND_ID_HANDLER(IDC_UPLOAD,OnUpload)
		COMMAND_ID_HANDLER(IDC_DOWNLOAD,OnDownload)
		COMMAND_ID_HANDLER(IDC_DELETE,OnDelete)
		COMMAND_ID_HANDLER(IDC_NEWFOLDER,OnNewFolder)
		COMMAND_ID_HANDLER(IDC_RENAME,OnRename)
		COMMAND_ID_HANDLER(IDC_REFRESH,OnRefresh)
		COMMAND_ID_HANDLER(IDC_LOGOUT,OnLogout)
		COMMAND_ID_HANDLER(IDC_SHOW,OnShow)
		NOTIFY_HANDLER_EX(IDC_LIST,NM_DBLCLK,OnDbClick)
		NOTIFY_HANDLER_EX(IDC_LIST,NM_CLICK,OnClick)
		NOTIFY_HANDLER_EX(IDC_LIST,NM_RCLICK,OnMenu)
        REFLECT_NOTIFICATIONS()
	END_MSG_MAP()

	LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnUploadStart(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnUploadFinish(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDownloadStart(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDownloadFinish(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnUpload(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnDownload(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnDelete(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnNewFolder(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnRename(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnRefresh(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnLogout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnShow(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
	LRESULT OnDbClick(LPNMHDR lParam);
	LRESULT OnClick(LPNMHDR lParam);
	LRESULT OnMenu(LPNMHDR lParam);

private:
	void FillView();
	void FillListView();
	void FillImageList(vector<CustomFileInfo>& files);
	void CloseDialog(int nVal);
	BOOL LoadPicFromFile(CString szFileName,IPicture** pPicture,HBITMAP* phBitmap,BOOL bLoadMemory,string& sData);
	void UpdateList();
	void UpdateInfo();
	void RefreshView();
	
	static DWORD __stdcall UploadHandler(LPVOID);
	static DWORD __stdcall DownloadHandler(LPVOID);

protected:
	CButton m_btnShow;
	CStatic m_sStateInfo;
	CListViewCtrl m_wndList;
	CImageList m_imgList;
	LIST_FS m_lFiles;
	string m_sTargetDirID;
	string m_sCurDirID;
//	string m_sParentDirID;
//	string m_sGParentDirID;
	stack<DirInfo>  m_sDirStack;
	UINT m_uCountDown;
	UINT m_uCountUp;
};
